package cn.alm.allinoneboot.service;

import cn.alm.allinoneboot.utils.JedisPoolUtil;
import cn.alm.allinoneboot.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;

@Service
public class ApiService {

    public Boolean tokenValid(String token) {
        if (!StringUtils.hasText(token)) {
            return false;
        }
        Claims claims = JwtUtil.parseJwt(token);
        String subject = claims.getSubject();
        Jedis jedis = JedisPoolUtil.getJedisPoolInstance().getResource();
        String s = jedis.get(subject);
        if (s == null) {
            return false;
        }
        if (!s.equals(token)) {
            return false;
        }
        jedis.expire(subject, 3600);
        return true;
    }
}
